package main.java.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * 数据库操作工具类
 */
public class MyDBUtil {

    /*
     * 获取数据库连接对象，管理数据库连接
     */
    public Connection getConnection() {
        try {
            String driver = "com.mysql.jdbc.Driver";
            String url = "jdbc:mysql://localhost:3306/sys?useUnicode=true&characterEncoding=UTF-8";
            String user = "root";
            String password = "root";
            Class.forName(driver);
            Connection conn = DriverManager.getConnection(url,user,password);
            if(conn != null){
                System.out.print("连接成功！！");
            }
            return conn;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
    /*
     * 管理数据关闭(归还连接)
     */
    public static void close(Connection conn) {
        if(conn!=null){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        MyDBUtil myDBUtil = new MyDBUtil();
        myDBUtil.getConnection();
    }
}
